<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-05-14 16:40:00
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2021-02-20 09:54:35
 */

namespace app\weixin_manage\model;
use app\common\model\BaseNew;

class WeixinMenu extends BaseNew {
	// 设置数据表（不含前缀）
	//protected $name = '';

	// 定义时间戳字段名
	protected $createTime = '';
	protected $updateTime = '';

	// 定义排序字段
	protected $_orderField = 'vieworder';
	protected $_orderDesc = 'desc';

	//获取父级菜单列表
	public function getCategorys($wid) {
		$map = array(
			'wid'       => $wid,
			'isopen'    => 1,
			'parentid'  => 0,
		);
		$data = $this->where($map)->order($this->_orderField.' desc')->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseSearch($data);
	}

	//获取菜单
	public function getMenu($wid) {
		$map = array(
			'wid' => $wid,
			'isopen' => 1,
		);
		$data = $this->where($map)->order($this->_orderField.' desc')->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseDataArray($data);
	}

	// $newmenu =  array(
	//  "button" => array(
	//      0 => array (
	//          'name' => '昆山便民',
	//          'sub_button' => array (
	//              array('type'=>'view','name'=>'昆山民生','url'=>'http://www.ksbbs.com/m/thread.php?fid=113'),
	//          ),
	//      ),
	//      1 => array (
	//          'name' => '论坛互动',
	//          'sub_button' => array (
	//              array('type'=>'view','name'=>'昆山民生','url'=>'http://www.ksbbs.com/m/thread.php?fid=113'),
	//          ),
	//      ),
	//      2 => array (
	//          'type' => 'view',
	//          'name' => '晒花草',
	//          'url'  => 'http://www.ksbbs.com/m/read.php?tid=4383208',
	//      ),
	//  ),
	// );

	function parseDataArray($data) {
		$parent = $sub = array();
		foreach ($data as $key => $value) {
			if ($value['event'] == 'view') {
				$tmp = array(
					'type'  => $value['event'],
					'name'  => $value['name'],
					'url'   => $value['keyword'],
				);
			} else if ($value['event'] == 'click') {
				$tmp = array(
					'type'      => $value['event'],
					'name'      => $value['name'],
					'key'       => $value['keyword'],
				);
			} else if ($value['event'] == 'miniprogram') {
				$tmp = array(
					'type'      => $value['event'],
					"name"      => $value['name'],
					"url"       => $value['keyword'],
					'appid'     => $value['appid'],
					'pagepath'  => $value['pagepath'],
				);
			} else {
				$tmp = array(
					'name'  => $value['name'],
				);
			}

			if ($value['parentid']) {
				$sub[$value['parentid']][] = $tmp;
			} else {
				$parent[$value['id']] = $tmp;
				// if ($value['type'] == 'sub') {
				//  $tmp = array(
				//      'name'  => $value['name'],
				//  );
				//  $parent[$value['id']] = $tmp;
				// } else {
				//  $parent[$value['id']] = $tmp;
				// }
			}
		}
		$list = array();
		foreach ($parent as $key => $value) {
			if ($sub[$key]) {
				$value['sub_button'] = $sub[$key];
			}
			$list[] = $value;
		}
		return array('button'=>$list);
	}

	public function getCount() {
		$data = $this->count();
		return $data;
	}

	public function countSearch($map) {
		return $this->where($map)->count();
	}

	public function search($map, $limit, $orderBy='') {
		if (!$orderBy) {
			$orderBy = array($this->_orderField=>$this->_orderDesc);
		}
		$data = $this->where($map)->order($orderBy)->limit($limit)->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseSearch($data);
	}

	protected function parseSearch($data) {
		$ftpWeb = config('app.ftp_web');
		$isopen_config = config('isopen_config');

		$list = $ids = array();
		foreach ($data as $key => $value) {
			//通用解析
			//$value['create_time_show'] = $value['create_time'] ? date("Y-m-d H:i:s", $value['create_time']) : '';
			//$value['status_show'] = isset($value['status']) ? $isopen_config[$value['status']] : '';
			//$value['picurl'] = $value['picurl'] ? $ftpWeb.$value['picurl'] : '';
			//通用解析

			$list[$value['id']] = $value;
			$ids[$value['id']] = $value['id'];
		}
		return array($list, $ids);
	}

	protected function parseInfo($info) {
		$ftpWeb = config('app.ftp_web');
		$isopen_config = config('isopen_config');

		//通用解析
		//$info['create_time_show'] = $info['create_time'] ? date("Y-m-d H:i:s", $info['create_time']) : '';
		//$info['status_show'] = isset($info['status']) ? $isopen_config[$info['status']] : '';
		//$info['picurl'] = $info['picurl'] ? $ftpWeb.$info['picurl'] : '';
		//通用解析

		return $info;
	}

}